<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  
  <title>windows7使用Sphinx+PHP+MySQL详细介绍 | 程序小工  | Just do it!</title>
  <meta name="description" content="遇见更好的自己~" />
  <meta name="keywords" content="" />
  <meta name="HandheldFriendly" content="True" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <link rel="shortcut icon" href="/">
  <link rel="alternate" href="/atom.xml" title="程序小工  | Just do it!">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="由于业务需要，需要做类似淘宝商城商品检索的功能，对于数据量很大的情况，MySQL 查询的效率损耗很大，需要使用专门的索引引擎进行搜索查询，实现功能，对于和 PHP 和 Mysql 的结合的索引引擎中， xunsearch 和 sphinx 是较为著名的，但由于 xunsearch 服务器端不支持 windows，所以暂且先考虑 sphinx 的使用。sphinx 目前已支持简体中文、繁体中文和英文">
<meta name="keywords" content="thinkphp5,sphinx">
<meta property="og:type" content="article">
<meta property="og:title" content="windows7使用Sphinx+PHP+MySQL详细介绍">
<meta property="og:url" content="http://blog.i2arch.tech/2018/05/14/sphinx-php-windows/index.html">
<meta property="og:site_name" content="程序小工  | Just do it!">
<meta property="og:description" content="由于业务需要，需要做类似淘宝商城商品检索的功能，对于数据量很大的情况，MySQL 查询的效率损耗很大，需要使用专门的索引引擎进行搜索查询，实现功能，对于和 PHP 和 Mysql 的结合的索引引擎中， xunsearch 和 sphinx 是较为著名的，但由于 xunsearch 服务器端不支持 windows，所以暂且先考虑 sphinx 的使用。sphinx 目前已支持简体中文、繁体中文和英文">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-08-14T07:59:24.562Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="windows7使用Sphinx+PHP+MySQL详细介绍">
<meta name="twitter:description" content="由于业务需要，需要做类似淘宝商城商品检索的功能，对于数据量很大的情况，MySQL 查询的效率损耗很大，需要使用专门的索引引擎进行搜索查询，实现功能，对于和 PHP 和 Mysql 的结合的索引引擎中， xunsearch 和 sphinx 是较为著名的，但由于 xunsearch 服务器端不支持 windows，所以暂且先考虑 sphinx 的使用。sphinx 目前已支持简体中文、繁体中文和英文">
    
  <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
  <link href='//cdn.bootcss.com/node-waves/0.7.5/waves.min.css' rel='stylesheet'>
  <link rel="stylesheet" href="/style.css">
  <script>
    function setLoadingBarProgress(num) {
      document.getElementById('loading-bar').style.width=num+"%";
    }
  </script>
</head>

<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script>
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				程序小工  | Just do it!
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-home' href='/'>
								<i class='icon icon-list2'></i>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								<i class='icon icon-bookmark'></i>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								<i class='icon icon-user'></i>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>

		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <!-- 左侧内容 -->
      <div class='l_main'>
        <article id="post-sphinx-php-windows"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>

	<h2 class="title">
  	<a href="/2018/05/14/sphinx-php-windows/">
    	windows7使用Sphinx+PHP+MySQL详细介绍
    </a>
	</h2>

	<div class="pagenav">
		<time>
			2018-05-14
		</time>

		
    
    <div class='cats'>
        <i class="icon icon-bookmark"></i>
        <a href="/categories/PHP/">PHP</a>
    </div>


		
			<div class="pagetags">
				<i class="icon icon-price-tag" style="margin-right: 2px;"></i>
				
					<a href="/tags/thinkphp5/" style="margin-left: 3px;">
						thinkphp5
					</a>
				
					<a href="/tags/sphinx/" style="margin-left: 3px;">
						sphinx
					</a>
				
			</div>
			
	</div>
	</section>

	
	<section class="toc-wrapper"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一-安装windows"><span class="toc-text"> 一、安装（Windows）</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1官方下载"><span class="toc-text"> 1.官方下载</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2解压并重命名"><span class="toc-text"> 2.解压并重命名</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3文件夹目录介绍"><span class="toc-text"> 3.文件夹目录介绍</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4设置配置文件"><span class="toc-text"> 4.设置配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5操作数据库导入样例数据"><span class="toc-text"> 5.操作数据库，导入样例数据</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6生成索引文件"><span class="toc-text"> 6.生成索引文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7开启搜索服务保持后台运行"><span class="toc-text"> 7.开启搜索服务，保持后台运行</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二-php-开启-sphinx-扩展"><span class="toc-text"> 二、PHP 开启 sphinx 扩展</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1下载-php_sphinx-扩展-前往"><span class="toc-text"> 1.下载 php_sphinx 扩展: 前往</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2修改-phpini-配置文件"><span class="toc-text"> 2.修改 php.ini 配置文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3在-phpinfophp-输出的信息中查看-sphinx-扩展是否安装成功"><span class="toc-text"> 3.在 phpinfo.php 输出的信息中查看 sphinx 扩展是否安装成功</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三-代码实现"><span class="toc-text"> 三、代码实现</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1样例数据表testdocuments记录"><span class="toc-text"> 1.样例数据表test.documents记录：</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2php-代码实现"><span class="toc-text"> 2.PHP 代码实现</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#一般实现"><span class="toc-text"> 一般实现</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#thinkphp5-使用介绍"><span class="toc-text"> thinkphp5 使用介绍</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#四-在-thinkphp5-项目中应用"><span class="toc-text"> 四、在 ThinkPHP5 项目中应用</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1修改配置信息sphinxbinsphinxconf"><span class="toc-text"> 1.修改配置信息sphinx/bin/sphinx.conf</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2生成索引并开启-searchd-服务"><span class="toc-text"> 2.生成索引，并开启 searchd 服务</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3程序实现"><span class="toc-text"> 3.程序实现</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4测试实现"><span class="toc-text"> 4.测试实现</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#五-扩展补充"><span class="toc-text"> 五、扩展补充</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#六-参考资料"><span class="toc-text"> 六、参考资料：</span></a></li></ol></section>
	

	<section class="article typo">
		<div class="article-entry" itemprop="articleBody">
				<span class="zhaiyao">[摘要]</span>
			<p>由于业务需要，需要做类似淘宝商城商品检索的功能，对于数据量很大的情况，MySQL 查询的效率损耗很大，需要使用专门的索引引擎进行搜索查询，实现功能，对于和 PHP 和 Mysql 的结合的索引引擎中， xunsearch 和 sphinx 是较为著名的，但由于 xunsearch 服务器端不支持 windows，所以暂且先考虑 sphinx 的使用。sphinx 目前已支持简体中文、繁体中文和英文的检索，不需要额外安装插件支持。</p>
<a id="more"></a>
<h2 id="一-安装windows"><a class="markdownIt-Anchor" href="#一-安装windows"></a> 一、安装（Windows）</h2>
<h3 id="1官方下载"><a class="markdownIt-Anchor" href="#1官方下载"></a> <strong>1.官方下载</strong></h3>
<p>Sphinx 下载地址： <a href="http://sphinxsearch.com/downloads/release/" target="_blank" rel="noopener">下载</a></p>
<h3 id="2解压并重命名"><a class="markdownIt-Anchor" href="#2解压并重命名"></a> <strong>2.解压并重命名</strong></h3>
<p>此处下载版本为<code>3.0.3</code>，将 sphinx 文件夹命名为<code>sphinx</code></p>
<h3 id="3文件夹目录介绍"><a class="markdownIt-Anchor" href="#3文件夹目录介绍"></a> <strong>3.文件夹目录介绍</strong></h3>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">sphinx</span><br><span class="line">--api(各语言支持的api)</span><br><span class="line">--bin（二进制程序）</span><br><span class="line">--doc（文档说明）</span><br><span class="line">--etc（配置文件：conf/sql）</span><br><span class="line">--misc</span><br><span class="line">--src</span><br><span class="line"><span class="comment"># 手动创建以下两个文件夹</span></span><br><span class="line">--data</span><br><span class="line">--log</span><br></pre></td></tr></table></figure>
<h3 id="4设置配置文件"><a class="markdownIt-Anchor" href="#4设置配置文件"></a> <strong>4.设置配置文件</strong></h3>
<p>(1)将<code>sphinx/etc/sphinx-min.conf.dist</code>文件复制到<code>sphinx/bin/</code>目录下，并重命名为<code>sphinx.conf</code></p>
<p>注：<code>sphinx/etc/sphinx.conf.dist</code>为带注释的详细的</p>
<p>(2)设置配置项</p>
<p>主要是以下为配置函数：</p>
<blockquote>
<p>source src1{} — 设置索引源(数据库的基本配置和数据表)</p>
</blockquote>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 连接的数据库类型</span></span><br><span class="line"><span class="attr">type</span> = mysql</span><br><span class="line"><span class="comment"># 连接的数据库主机</span></span><br><span class="line"><span class="attr">sql_host</span> = localhost</span><br><span class="line"><span class="comment"># 数据库连接的用户名，默认为test</span></span><br><span class="line"><span class="attr">sql_user</span> = root</span><br><span class="line"><span class="comment"># 数据库连接的密码，默认为空</span></span><br><span class="line"><span class="attr">sql_pass</span> =<span class="number">123123</span></span><br><span class="line"><span class="comment"># 连接的数据库名称，默认为test</span></span><br><span class="line"><span class="attr">sql_db</span> = test</span><br><span class="line"><span class="comment"># 连接数据库的端口号，默认为3306</span></span><br><span class="line"><span class="attr">sql_port</span> = <span class="number">3306</span></span><br><span class="line"><span class="comment"># 操作的数据表执行的查询语句</span></span><br><span class="line"><span class="attr">sql_query</span> = \</span><br><span class="line">    SELECT id, group_id, UNIX_TIMESTAMP(date_added) AS date_added, title, content \</span><br><span class="line">    FROM documents</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置查询过滤条件</span></span><br><span class="line"><span class="attr">sql_attr_uint</span> = group_id</span><br></pre></td></tr></table></figure>
<blockquote>
<p>index test1{} — 索引文件</p>
</blockquote>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 指定索引源</span></span><br><span class="line"><span class="attr">source</span> = src1</span><br><span class="line"><span class="comment"># 索引数据存放目录，默认为/var/data/test1</span></span><br><span class="line"><span class="attr">path</span> = D:\Service\sphinx\data\test1</span><br><span class="line"></span><br><span class="line"><span class="comment"># 设置中文匹配</span></span><br><span class="line"><span class="attr">min_word_len</span>    = <span class="number">1</span></span><br><span class="line"><span class="comment"># 指定字符集（已废弃）</span></span><br><span class="line"><span class="attr">charset_type</span>    = utf-<span class="number">8</span></span><br><span class="line"><span class="comment"># 指定utf-8的编码表</span></span><br><span class="line"><span class="attr">charset_table</span>   = <span class="number">0</span>..<span class="number">9</span>, A..Z-&gt;a..z, _, a..z, U+<span class="number">410</span>..U+<span class="number">42</span>F-&gt;U+<span class="number">430</span>..U+<span class="number">44</span>F, U+<span class="number">430</span>..U+<span class="number">44</span>F</span><br><span class="line"></span><br><span class="line"><span class="attr">min_prefix_len</span> = <span class="number">0</span></span><br><span class="line"><span class="attr">min_infix_len</span> = <span class="number">1</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启中文分词支持</span></span><br><span class="line"><span class="attr">ngram_len</span> = <span class="number">1</span></span><br><span class="line"><span class="comment"># 需要分词的字符</span></span><br><span class="line"><span class="attr">ngram_chars</span> = U+<span class="number">3000</span>..U+<span class="number">2</span>FA1F</span><br></pre></td></tr></table></figure>
<blockquote>
<p>index test1stemmed : test1{}</p>
</blockquote>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 主要需要修改的配置项，默认为/var/data/test1stemmed</span></span><br><span class="line"></span><br><span class="line"><span class="attr">path</span> = D:\Service\sphinx\data\test1stemmed</span><br></pre></td></tr></table></figure>
<blockquote>
<p>index rt{}</p>
</blockquote>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 主要需要修改的配置项，默认为/var/data/rt</span></span><br><span class="line"><span class="attr">path</span> = D:\Service\sphinx\data\rt</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定对哪些字段进行匹配</span></span><br><span class="line"><span class="attr">rt_field</span> = name</span><br><span class="line"><span class="attr">rt_field</span> = ename</span><br><span class="line"><span class="attr">rt_field</span> = setmeal</span><br><span class="line"><span class="attr">rt_field</span> = category</span><br><span class="line"><span class="attr">rt_field</span> = country</span><br><span class="line"><span class="attr">rt_field</span> = traffic</span><br><span class="line"><span class="attr">rt_field</span> = body</span><br><span class="line"></span><br><span class="line"><span class="attr">rt_attr_uint</span> = <span class="literal">off</span>erid</span><br></pre></td></tr></table></figure>
<blockquote>
<p>searchd{}</p>
</blockquote>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 自定义日志文件位置</span></span><br><span class="line"><span class="attr">log</span> = D:\Service\sphinx\log\searchd.log</span><br><span class="line"><span class="attr">query_log</span> = D:\Service\sphinx\log\query.log</span><br><span class="line"><span class="attr">pid_file</span> = D:\Service\sphinx\log\searchd.pid</span><br></pre></td></tr></table></figure>
<p>以下几项不需要修改默认值，即可直接使用</p>
<blockquote>
<p>source src1throttled : src1{}</p>
</blockquote>
<p>分布式索引的相关配置，没有则可以不修改</p>
<blockquote>
<p>index dist1{}<br>
indexer{}<br>
common{}</p>
</blockquote>
<p>【注】：主要的配置是<code>source</code> <code>index</code> <code>indexer</code> <code>searchd</code>, 其他几项可以不需要</p>
<h3 id="5操作数据库导入样例数据"><a class="markdownIt-Anchor" href="#5操作数据库导入样例数据"></a> <strong>5.操作数据库，导入样例数据</strong></h3>
<p>(1)进入到 mysql 命令行，执行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">D:\phpStudy\PHPTutorial\MySQL\bin&gt;mysql -uroot -p</span><br><span class="line">Enter password: *************</span><br><span class="line">Welcome to the MySQL monitor.  Commands end with ; or \g.</span><br><span class="line">Your MySQL connection id is 12</span><br><span class="line">Server version: 5.5.53 MySQL Community Server (GPL)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">mysql&gt; use <span class="built_in">test</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 恢复样例数据到数据库</span></span><br><span class="line">mysql&gt; <span class="built_in">source</span> /D:\Service\sphinx\etc/eaxmple.sql</span><br><span class="line"></span><br><span class="line"><span class="comment"># 新增两个数据表，documents和tags</span></span><br><span class="line">mysql&gt; show tables;</span><br><span class="line">documents</span><br><span class="line">tags</span><br></pre></td></tr></table></figure>
<h3 id="6生成索引文件"><a class="markdownIt-Anchor" href="#6生成索引文件"></a> <strong>6.生成索引文件</strong></h3>
<p>cmd 命令行进入到<code>sphinx/bin/</code>目录下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成索引文件,本地重新构建--rotate</span></span><br><span class="line">&gt; indexer.exe --config sphinx.conf --all --rotate</span><br><span class="line">Sphinx 3.0.3-dev (commit facc3fb)</span><br><span class="line">Copyright (c) 2001-2018, Andrew Aksyonoff</span><br><span class="line">Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)</span><br><span class="line"></span><br><span class="line">using config file <span class="string">'sphinx.conf'</span>...</span><br><span class="line">WARNING: key <span class="string">'docinfo'</span> was permanently removed from Sphinx configuration. Refer to documentation <span class="keyword">for</span> details.</span><br><span class="line">WARNING: key <span class="string">'dict'</span> was permanently removed from Sphinx configuration. Refer to documentation <span class="keyword">for</span> details.</span><br><span class="line">WARNING: key <span class="string">'mva_updates_pool'</span> was permanently removed from Sphinx configuration. Refer to documentation <span class="keyword">for</span> details.</span><br><span class="line">indexing index <span class="string">'test1'</span>...</span><br><span class="line">collected 4 docs, 0.0 MB</span><br><span class="line">sorted 0.0 Mhits, 100.0% <span class="keyword">done</span></span><br><span class="line">total 4 docs, 0.2 Kb</span><br><span class="line">total 1.0 sec, 0.2 Kb/sec, 3 docs/sec</span><br><span class="line">indexing index <span class="string">'test1stemmed'</span>...</span><br><span class="line">collected 4 docs, 0.0 MB</span><br><span class="line">sorted 0.0 Mhits, 100.0% <span class="keyword">done</span></span><br><span class="line">total 4 docs, 0.2 Kb</span><br><span class="line">total 1.0 sec, 0.2 Kb/sec, 3 docs/sec</span><br><span class="line">skipping non-plain index <span class="string">'dist1'</span>...</span><br><span class="line">skipping non-plain index <span class="string">'rt'</span>...</span><br></pre></td></tr></table></figure>
<p>【注】新版 sphinx 的 bin 目录下已经没有 search.exe 程序，所以不能直接在命令行执行返回结果，只能使用 api 接口返回数据。</p>
<h3 id="7开启搜索服务保持后台运行"><a class="markdownIt-Anchor" href="#7开启搜索服务保持后台运行"></a> <strong>7.开启搜索服务，保持后台运行</strong></h3>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&gt; searchd.exe --pidfile</span><br><span class="line"></span><br><span class="line">[Tue May 15 09:02:14.690 2018] [7776] using config file <span class="string">'./sphinx.conf'</span>...</span><br><span class="line">listening on all interfaces, port=9312</span><br><span class="line">listening on all interfaces, port=9306</span><br><span class="line">Sphinx 3.0.3-dev (commit facc3fb)</span><br><span class="line">Copyright (c) 2001-2018, Andrew Aksyonoff</span><br><span class="line">Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)</span><br></pre></td></tr></table></figure>
<h2 id="二-php-开启-sphinx-扩展"><a class="markdownIt-Anchor" href="#二-php-开启-sphinx-扩展"></a> 二、PHP 开启 sphinx 扩展</h2>
<h3 id="1下载-php_sphinx-扩展-前往"><a class="markdownIt-Anchor" href="#1下载-php_sphinx-扩展-前往"></a> <strong>1.下载 php_sphinx 扩展</strong>: <a href="https://pecl.php.net/package/sphinx/1.3.2/windows" target="_blank" rel="noopener">前往</a></h3>
<p>具体需要下载的版本需要查看 phpinfo 信息:</p>
<pre><code>Architecture         ==》x86/x64
PHP Extension Build  ==》NTS/NS
</code></pre>
<p>下载并解压后，将<code>php_sphinx.dll</code>文件放到 php/ext 目录下</p>
<h3 id="2修改-phpini-配置文件"><a class="markdownIt-Anchor" href="#2修改-phpini-配置文件"></a> <strong>2.修改 php.ini 配置文件</strong></h3>
<figure class="highlight ini"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在 Dynamic Extensions 列表中添加php_sphinx扩展</span></span><br><span class="line"><span class="attr">extension</span>=php_sphinx.dll</span><br></pre></td></tr></table></figure>
<p>修改后重启 apache 服务</p>
<h3 id="3在-phpinfophp-输出的信息中查看-sphinx-扩展是否安装成功"><a class="markdownIt-Anchor" href="#3在-phpinfophp-输出的信息中查看-sphinx-扩展是否安装成功"></a> <strong>3.在 phpinfo.php 输出的信息中查看 sphinx 扩展是否安装成功</strong></h3>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">            sphinx</span><br><span class="line">sphinx support	 enabled</span><br><span class="line">Version	         1.3.2</span><br><span class="line">Revision	 $Revision$</span><br></pre></td></tr></table></figure>
<h2 id="三-代码实现"><a class="markdownIt-Anchor" href="#三-代码实现"></a> 三、代码实现</h2>
<h3 id="1样例数据表testdocuments记录"><a class="markdownIt-Anchor" href="#1样例数据表testdocuments记录"></a> 1.样例数据表<code>test.documents</code>记录：</h3>
<table>
<thead>
<tr>
<th>id</th>
<th>group_id</th>
<th>group_id2</th>
<th style="text-align:left">date_added</th>
<th style="text-align:left">title</th>
<th style="text-align:left">content</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
<td>5</td>
<td style="text-align:left">2018-05-14 09:12:25</td>
<td style="text-align:left">test one</td>
<td style="text-align:left">this is my test document number one. also checking search within phrases.</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>6</td>
<td style="text-align:left">2018-05-14 09:12:25</td>
<td style="text-align:left">test two</td>
<td style="text-align:left">this is my test document number two</td>
</tr>
<tr>
<td>3</td>
<td>2</td>
<td>7</td>
<td style="text-align:left">2018-05-14 09:12:25</td>
<td style="text-align:left">another doc</td>
<td style="text-align:left">this is another group</td>
</tr>
<tr>
<td>4</td>
<td>2</td>
<td>8</td>
<td style="text-align:left">2018-05-14 09:12:25</td>
<td style="text-align:left">doc number four</td>
<td style="text-align:left">this is to test groups</td>
</tr>
</tbody>
</table>
<h3 id="2php-代码实现"><a class="markdownIt-Anchor" href="#2php-代码实现"></a> 2.PHP 代码实现</h3>
<h4 id="一般实现"><a class="markdownIt-Anchor" href="#一般实现"></a> <strong>一般实现</strong></h4>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?php</span></span><br><span class="line"><span class="keyword">require</span>(<span class="string">'sphinxapi.php'</span>);</span><br><span class="line"></span><br><span class="line">$sphinx = <span class="keyword">new</span> SphinxClient();</span><br><span class="line">$sphinx-&gt;SetServer(<span class="string">'localhost'</span>,<span class="number">9312</span>);</span><br><span class="line">$sphinx-&gt;SetMatchMode(SPH_MATCH_ANY);</span><br><span class="line">$sphinx-&gt;SetArrayResult ( <span class="keyword">true</span> );</span><br><span class="line">$res = $sphinx-&gt;Query($_GET[<span class="string">'key'</span>],<span class="string">'*'</span>);</span><br><span class="line">var_dump($res);</span><br></pre></td></tr></table></figure>
<h4 id="thinkphp5-使用介绍"><a class="markdownIt-Anchor" href="#thinkphp5-使用介绍"></a> <strong>thinkphp5 使用介绍</strong></h4>
<p>1.将<code>sphinxapi.php</code>文件放到<code>extend</code>目录下</p>
<p>2.在控制器方法中使用（<code>app/api/index</code>）</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $sphinx = <span class="keyword">new</span> \SphinxClient();</span><br><span class="line">    <span class="comment">// sphinx的主机名和端口</span></span><br><span class="line">    $sphinx-&gt;SetServer(<span class="string">'localhost'</span>,<span class="number">9312</span>);</span><br><span class="line">    <span class="comment">// 匹配模式</span></span><br><span class="line">    $sphinx-&gt;SetMatchMode(SPH_MATCH_ANY);</span><br><span class="line">    <span class="comment">// 设置返回结果集为php数组格式</span></span><br><span class="line"></span><br><span class="line">    $sphinx-&gt;SetArrayResult ( <span class="keyword">true</span> );</span><br><span class="line">    $res = $sphinx-&gt;Query(input(<span class="string">'key'</span>),<span class="string">'*'</span>);</span><br><span class="line">    var_dump($res);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>3.url 访问：<br>
<code>http://localhost/mypro/api/index/test?key=test</code></p>
<p>4.输出数据</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line">D:\web\COD\api\application\api\controller\Index.php:21:</span><br><span class="line">array (size=10)</span><br><span class="line">  'error' =&gt; string '' (length=0)</span><br><span class="line">  'warning' =&gt; string '' (length=0)</span><br><span class="line">  'status' =&gt; int 0</span><br><span class="line">  'fields' =&gt;</span><br><span class="line">    // 查询显示的字段名</span><br><span class="line">    array (size=2)</span><br><span class="line">      0 =&gt; string 'title' (length=5)</span><br><span class="line">      1 =&gt; string 'content' (length=7)</span><br><span class="line">  'attrs' =&gt;</span><br><span class="line">    array (size=2)</span><br><span class="line">      'group_id' =&gt; string '1' (length=1)</span><br><span class="line">      'date_added' =&gt; string '2' (length=1)</span><br><span class="line">  'matches' =&gt;</span><br><span class="line">    // 匹配的结果，返回匹配记录的id和权重（权重越大，匹配条件越多）</span><br><span class="line">    array (size=3)</span><br><span class="line">      0 =&gt;</span><br><span class="line">        array (size=3)</span><br><span class="line">          'id' =&gt; string '1' (length=1)</span><br><span class="line">          'weight' =&gt; int 2421</span><br><span class="line">          'attrs' =&gt;</span><br><span class="line">            array (size=2)</span><br><span class="line">              ...</span><br><span class="line">      1 =&gt;</span><br><span class="line">        array (size=3)</span><br><span class="line">          'id' =&gt; string '2' (length=1)</span><br><span class="line">          'weight' =&gt; int 2421</span><br><span class="line">          'attrs' =&gt;</span><br><span class="line">            array (size=2)</span><br><span class="line">              ...</span><br><span class="line">      2 =&gt;</span><br><span class="line">        array (size=3)</span><br><span class="line">          'id' =&gt; string '4' (length=1)</span><br><span class="line">          'weight' =&gt; int 1442</span><br><span class="line">          'attrs' =&gt;</span><br><span class="line">            array (size=2)</span><br><span class="line">              ...</span><br><span class="line">  'total' =&gt; int 3</span><br><span class="line">  'total_found' =&gt; int 3</span><br><span class="line">  'time' =&gt; float 0</span><br><span class="line">  'words' =&gt;</span><br><span class="line">    array (size=1)</span><br><span class="line">      'test' =&gt;</span><br><span class="line">        array (size=2)</span><br><span class="line">          'docs' =&gt; int 6</span><br><span class="line">          'hits' =&gt; int 10</span><br></pre></td></tr></table></figure>
<h2 id="四-在-thinkphp5-项目中应用"><a class="markdownIt-Anchor" href="#四-在-thinkphp5-项目中应用"></a> 四、在 ThinkPHP5 项目中应用</h2>
<h3 id="1修改配置信息sphinxbinsphinxconf"><a class="markdownIt-Anchor" href="#1修改配置信息sphinxbinsphinxconf"></a> <strong>1.修改配置信息</strong><code>sphinx/bin/sphinx.conf</code></h3>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">source src1&#123;</span><br><span class="line"><span class="comment"># 省略其他配置</span></span><br><span class="line"><span class="attr">sql_user</span>  = root</span><br><span class="line"><span class="attr">sql_pass</span>  = <span class="number">123123</span></span><br><span class="line"><span class="attr">sql_db</span>    = shopMall</span><br><span class="line"><span class="attr">sql_query</span> = \</span><br><span class="line">  SELECT id,offerid, name, ename, setmeal, category, country, traffic, os, body, inventory_title, shop \</span><br><span class="line">  FROM i_offer</span><br><span class="line"><span class="attr">sql_attr_uint</span> = <span class="literal">off</span>erid</span><br><span class="line"><span class="comment"># 省略其他配置</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">index rt</span><br><span class="line">&#123;</span><br><span class="line">  type  = rt</span><br><span class="line">  path  = D:\Service\sphinx\data\rt</span><br><span class="line"></span><br><span class="line">  rt_field  = name</span><br><span class="line">  rt_field  = ename</span><br><span class="line">  rt_field  = setmeal</span><br><span class="line">  rt_field  = category</span><br><span class="line">  rt_field  = country</span><br><span class="line">  rt_field  = traffic</span><br><span class="line">  rt_field  = body</span><br><span class="line"></span><br><span class="line">  rt_attr_uint  = offerid</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="2生成索引并开启-searchd-服务"><a class="markdownIt-Anchor" href="#2生成索引并开启-searchd-服务"></a> <strong>2.生成索引，并开启 searchd 服务</strong></h3>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 生成项目索引</span></span><br><span class="line">sphinx/bin/indexer.exe --config sphinx.conf --all</span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启服务 &amp;表示后台开启，不用保持窗口执行状态</span></span><br><span class="line">sphinx/bin/searchd.exe &amp;</span><br></pre></td></tr></table></figure>
<h3 id="3程序实现"><a class="markdownIt-Anchor" href="#3程序实现"></a> <strong>3.程序实现</strong></h3>
<p>sphinx 查询返回的结果并不是我们需要的显示结果，所以还需要对结果进行处理，从而获取到我们需要的结果。</p>
<p>默认 sphinx 返回的数据中包含 id 信息是和数据记录的信息是相关的，所以我们需要通过 id 到数据库中查询相关信息。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $s = <span class="keyword">new</span> \SphinxClient;</span><br><span class="line">    $s-&gt;setServer(<span class="string">"localhost"</span>, <span class="number">9312</span>);</span><br><span class="line">    <span class="comment">// 作为数组返回</span></span><br><span class="line">    $s-&gt;SetArrayResult(<span class="keyword">true</span>);</span><br><span class="line">    <span class="comment">// 匹配格式  任意匹配</span></span><br><span class="line">    $s-&gt;setMatchMode(SPH_MATCH_ANY);</span><br><span class="line">    $s-&gt;setMaxQueryTime(<span class="number">3</span>);</span><br><span class="line">    <span class="comment">// input()表示接收用户传过来的数据</span></span><br><span class="line">    $result = $s-&gt;query(input(<span class="string">'key'</span>),<span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> json($result);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="4测试实现"><a class="markdownIt-Anchor" href="#4测试实现"></a> <strong>4.测试实现</strong></h3>
<p>访问 url：<br>
<code>http://localhost/mypro/api/index/test?key=官方</code></p>
<p>返回结果：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br></pre></td><td class="code"><pre><span class="line">D:\web\COD\api\application\api\controller\Index.php:22:</span><br><span class="line">array (size=10)</span><br><span class="line">  'error' =&gt; string '' (length=0)</span><br><span class="line">  'warning' =&gt; string '' (length=0)</span><br><span class="line">  'status' =&gt; int 0</span><br><span class="line">  'fields' =&gt;</span><br><span class="line">    array (size=10)</span><br><span class="line">      0 =&gt; string 'name' (length=4)</span><br><span class="line">      1 =&gt; string 'ename' (length=5)</span><br><span class="line">      2 =&gt; string 'setmeal' (length=7)</span><br><span class="line">      3 =&gt; string 'category' (length=8)</span><br><span class="line">      4 =&gt; string 'country' (length=7)</span><br><span class="line">      5 =&gt; string 'traffic' (length=7)</span><br><span class="line">      6 =&gt; string 'os' (length=2)</span><br><span class="line">      7 =&gt; string 'body' (length=4)</span><br><span class="line">      8 =&gt; string 'inventory_title' (length=15)</span><br><span class="line">      9 =&gt; string 'shop' (length=4)</span><br><span class="line">  'attrs' =&gt;</span><br><span class="line">    array (size=1)</span><br><span class="line">      'offerid' =&gt; string '1' (length=1)</span><br><span class="line">  'matches' =&gt;</span><br><span class="line">    array (size=6)</span><br><span class="line">      0 =&gt;</span><br><span class="line">        array (size=3)</span><br><span class="line">          'id' =&gt; string '36' (length=2)</span><br><span class="line">          'weight' =&gt; int 4667</span><br><span class="line">          'attrs' =&gt;</span><br><span class="line">            array (size=1)</span><br><span class="line">              ...</span><br><span class="line">      1 =&gt;</span><br><span class="line">        array (size=3)</span><br><span class="line">          'id' =&gt; string '19' (length=2)</span><br><span class="line">          'weight' =&gt; int 2611</span><br><span class="line">          'attrs' =&gt;</span><br><span class="line">            array (size=1)</span><br><span class="line">              ...</span><br><span class="line">      // 此处省略部分数据</span><br><span class="line">  'total' =&gt; int 6</span><br><span class="line">  'total_found' =&gt; int 6</span><br><span class="line">  'time' =&gt; float 0</span><br><span class="line">  'words' =&gt;</span><br><span class="line">    array (size=2)</span><br><span class="line">      '官' =&gt;</span><br><span class="line">        array (size=2)</span><br><span class="line">          'docs' =&gt; int 14</span><br><span class="line">          'hits' =&gt; int 16</span><br><span class="line">      '方' =&gt;</span><br><span class="line">        array (size=2)</span><br><span class="line">          'docs' =&gt; int 70</span><br><span class="line">          'hits' =&gt; int 94</span><br></pre></td></tr></table></figure>
<p>对结果进行处理，支持过滤查询<code>SetFilter()</code>。</p>
<figure class="highlight php"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="function"><span class="keyword">function</span> <span class="title">test</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    $filterkey = <span class="string">'status'</span>;</span><br><span class="line">    $filtervalue = <span class="string">"1"</span>;</span><br><span class="line"></span><br><span class="line">    $sphinx = <span class="keyword">new</span> \SphinxClient;</span><br><span class="line">    $sphinx-&gt;setServer(<span class="string">"localhost"</span>, <span class="number">9312</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 进行过滤查询</span></span><br><span class="line">    <span class="keyword">if</span> (!<span class="keyword">empty</span>($filterkey)) &#123;</span><br><span class="line">        <span class="comment">// 只在status==1的记录中检索</span></span><br><span class="line">        $filterRes = $sphinx-&gt;SetFilter($filterkey, [$filtervalue]);</span><br><span class="line">        <span class="keyword">if</span> (!$filterRes) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 作为数组返回</span></span><br><span class="line">    $sphinx-&gt;SetArrayResult(<span class="keyword">true</span>);</span><br><span class="line">    <span class="comment">// 匹配格式  任意匹配</span></span><br><span class="line">    $sphinx-&gt;setMatchMode(SPH_MATCH_ANY);</span><br><span class="line">    $sphinx-&gt;setMaxQueryTime(<span class="number">3</span>);</span><br><span class="line">    <span class="comment">// input()表示接收用户传过来的数据</span></span><br><span class="line">    $result = $sphinx-&gt;query(input(<span class="string">'key'</span>),<span class="string">'*'</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 避免没有匹配记录时报错</span></span><br><span class="line">    <span class="keyword">if</span>(<span class="keyword">empty</span>($result[<span class="string">'matches'</span>])) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    $result = $result[<span class="string">'matches'</span>];</span><br><span class="line">    <span class="comment">// 返回数组中指定的id列, 返回结果为单列数组</span></span><br><span class="line">    $result = array_column($result, <span class="string">'id'</span>);</span><br><span class="line">    $list = model(<span class="string">'offer'</span>)</span><br><span class="line">    -&gt;field(<span class="string">'offerid, status, name, ename, setmeal, category, country, traffic, os, body, inventory_title, shop'</span>)</span><br><span class="line">    -&gt;where(<span class="keyword">array</span>(<span class="string">'id'</span> =&gt; <span class="keyword">array</span>(<span class="string">'in'</span>, $result)))</span><br><span class="line">    -&gt;select();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> json($list);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>返回结果</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">[</span><br><span class="line">  &#123;</span><br><span class="line">    offerid: 2332302,</span><br><span class="line">    status: 1,</span><br><span class="line">    name: "【官方站】減震隱形增高鞋墊（安全有效~秒增高5公分~）",</span><br><span class="line">    ename: "zenggaoxiedian",</span><br><span class="line">    setmeal: "日韓超夯，氣墊隱形增高墊，輕鬆增高5公分，透氣減震，抗菌防臭，藝人最愛！【可拆分，自由裁剪，均碼35-44可用】【超殺998三雙】",</span><br><span class="line">    category: "[&#123;"id":6,"name":"其他"&#125;,&#123;"id":7,"name":"商城"&#125;,&#123;"id":8,"name":"家庭用品\n"&#125;]",</span><br><span class="line">    country: "[&#123;"id":11,"name":"American Samoa"&#125;,&#123;"id":1,"name":"Andorra"&#125;,&#123;"id":8,"name":"Angola"&#125;,&#123;"id":5,"name":"Anguilla"&#125;,&#123;"id":10,"name":"Argentina"&#125;,&#123;"id":7,"name":"Armenia"&#125;,&#123;"id":12,"name":"Austria"&#125;]",</span><br><span class="line">    traffic: "[&#123;"id":2,"name":"16+"&#125;,&#123;"id":3,"name":"3G\/4G"&#125;,&#123;"id":4,"name":"Adult"&#125;]",</span><br><span class="line">    os: "[&#123;"id":1,"name":"3DS System Software"&#125;,&#123;"id":2,"name":"Android"&#125;,&#123;"id":13,"name":"BeOS"&#125;,&#123;"id":16,"name":"CentOS"&#125;]",</span><br><span class="line">    body: "123123123",</span><br><span class="line">    inventory_title: "隱形增高鞋墊B",</span><br><span class="line">    shop: "[&#123;"userid":77912776,"name":"myShop"&#125;]"</span><br><span class="line">  &#125;,</span><br><span class="line">  &#123;</span><br><span class="line">    offerid: 3308032,</span><br><span class="line">    status: 1,</span><br><span class="line">    name: "【官方站】電熱造型梳",</span><br><span class="line">    ename: "zaoxingshu",</span><br><span class="line">    setmeal: "長/短髮都適用！好梳好上手！亂翹髮尾一秒聽話！【人氣爆红款美髮救星】限時特價，加NT$300再得1件！！！",</span><br><span class="line">    category: "[&#123;"id":2,"name":"美容"&#125;,&#123;"id":4,"name":"日用品"&#125;,&#123;"id":6,"name":"其他"&#125;,&#123;"id":8,"name":"家庭用品\n"&#125;]",</span><br><span class="line">    country: "[&#123;"id":6,"name":"Albania"&#125;,&#123;"id":4,"name":"Antigua And Barbuda"&#125;]",</span><br><span class="line">    traffic: "[&#123;"id":3,"name":"3G\/4G"&#125;,&#123;"id":5,"name":"Adult Content"&#125;,&#123;"id":6,"name":"App Discovery Traffic"&#125;]",</span><br><span class="line">    os: "[&#123;"id":3,"name":"Android with AOKP"&#125;,&#123;"id":5,"name":"Android with Cyanogen Mod"&#125;,&#123;"id":6,"name":"Android with LiquidSmooth"&#125;,&#123;"id":7,"name":"Android with MIUI"&#125;]",</span><br><span class="line">    body: "123123123"</span><br><span class="line">    inventory_title: "NOVA多功能卷髮棒B",</span><br><span class="line">    shop: "[&#123;"userid":77912776,"name":"myShop"&#125;]"</span><br><span class="line">  &#125;</span><br><span class="line">]</span><br></pre></td></tr></table></figure>
<h2 id="五-扩展补充"><a class="markdownIt-Anchor" href="#五-扩展补充"></a> 五、扩展补充</h2>
<p>需要对多个不同表创建不同的检索的使用方法：</p>
<p>只需创建不同的索引源和索引，相应的对应好即可。</p>
<p>实际项目中的 sphinx 完整配置：</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br></pre></td><td class="code"><pre><span class="line">source keyword</span><br><span class="line">&#123;</span><br><span class="line">  type      = mysql</span><br><span class="line">  sql_host  = localhost</span><br><span class="line">  sql_user  = root</span><br><span class="line">  sql_pass  = 123</span><br><span class="line">  sql_db    = goods</span><br><span class="line">  sql_query_pre   = SET NAMES utf8</span><br><span class="line"></span><br><span class="line">  sql_query  = \</span><br><span class="line">    SELECT id,status,keyword \</span><br><span class="line">    FROM t_keyword</span><br><span class="line"></span><br><span class="line">  <span class="comment"># 设置查询条件</span></span><br><span class="line">  sql_attr_uint = status</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">index keyword</span><br><span class="line">&#123;</span><br><span class="line">  source  = keyword</span><br><span class="line">  path  = D:/UPUPW_ANK_W64/Database/data_sphinx/keyword</span><br><span class="line">  charset_type  = utf-8</span><br><span class="line">  charset_table = 0..9, A..Z-&gt;a..z, _, a..z, U+410..U+42F-&gt;U+430..U+44F, U+430..U+44F</span><br><span class="line">  ngram_len = 1</span><br><span class="line">  ngram_chars = U+3000..U+2FA1F</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">source collection</span><br><span class="line">&#123;</span><br><span class="line">  type        = mysql</span><br><span class="line">  sql_host    = localhost</span><br><span class="line">  sql_user    = root</span><br><span class="line">  sql_pass    = 123456</span><br><span class="line">  sql_db      = goods</span><br><span class="line">  sql_query   = \</span><br><span class="line">    SELECT id, status, title, offerid, keyword, searchid, fromname \</span><br><span class="line">    FROM t_collection</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">index collection</span><br><span class="line">&#123;</span><br><span class="line">  source    = collection</span><br><span class="line">  path      = D:/UPUPW_ANK_W64/Database/data_sphinx/collection</span><br><span class="line">  mlock     = 0</span><br><span class="line">  morphology      = none</span><br><span class="line">  min_word_len    = 1</span><br><span class="line">  charset_table   = 0..9, A..Z-&gt;a..z, _, a..z, U+410..U+42F-&gt;U+430..U+44F, U+430..U+44F</span><br><span class="line">  min_prefix_len  = 0</span><br><span class="line">  min_infix_len   = 1</span><br><span class="line">  ngram_len   = 1</span><br><span class="line">  ngram_chars   = U+3000..U+2FA1F</span><br><span class="line">  html_strip    = 0</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">source goods</span><br><span class="line">&#123;</span><br><span class="line">  type        = mysql</span><br><span class="line">  sql_host    = localhost</span><br><span class="line">  sql_user    = root</span><br><span class="line">  sql_pass    = 123455</span><br><span class="line">  sql_db      = goods</span><br><span class="line">  sql_query   = \</span><br><span class="line">    SELECT id, status, name, seller_region, keywords, offerid, searchid, detailid, keyword, fromname \</span><br><span class="line">    FROM t_goods</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">index goods</span><br><span class="line">&#123;</span><br><span class="line">  source    = goods</span><br><span class="line">  path      = D:/UPUPW_ANK_W64/Database/data_sphinx/goods</span><br><span class="line">  mlock     = 0</span><br><span class="line">  morphology      = none</span><br><span class="line">  min_word_len    = 1</span><br><span class="line">  charset_table   = 0..9, A..Z-&gt;a..z, _, a..z, U+410..U+42F-&gt;U+430..U+44F, U+430..U+44F</span><br><span class="line">  min_prefix_len  = 0</span><br><span class="line">  min_infix_len   = 1</span><br><span class="line">  ngram_len       = 1</span><br><span class="line">  ngram_chars     = U+3000..U+2FA1F</span><br><span class="line">  html_strip      = 0</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">indexer</span><br><span class="line">&#123;</span><br><span class="line">  mem_limit   = 128M</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">searchd</span><br><span class="line">&#123;</span><br><span class="line">  listen    = 9312</span><br><span class="line">  <span class="comment">#listen   = 9306:mysql41</span></span><br><span class="line">  log   = D:/UPUPW_ANK_W64/Logs/sphinx.log</span><br><span class="line">  query_log       = D:/UPUPW_ANK_W64/Modules/Sphinx/log/query.log</span><br><span class="line">  read_timeout    = 5</span><br><span class="line">  client_timeout  = 300</span><br><span class="line">  max_children    = 30</span><br><span class="line">  persistent_connections_limit  = 30</span><br><span class="line">  pid_file        = D:/UPUPW_ANK_W64/Modules/Sphinx/log/searchd.pid</span><br><span class="line">  seamless_rotate = 1</span><br><span class="line">  preopen_indexes = 1</span><br><span class="line">  unlink_old      = 1</span><br><span class="line">  max_packet_size = 8M</span><br><span class="line">  max_filters     = 256</span><br><span class="line">  max_filter_values = 4096</span><br><span class="line">  max_batch_queries = 32</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="六-参考资料"><a class="markdownIt-Anchor" href="#六-参考资料"></a> 六、参考资料：</h2>
<blockquote>
<p>PHP 官方手册使用 Sphinx 介绍：</p>
</blockquote>
<p><a href="http://www.php.net/manual/zh/book.sphinx.php" target="_blank" rel="noopener">http://www.php.net/manual/zh/book.sphinx.php</a></p>
<blockquote>
<p>sphinx 安装：</p>
</blockquote>
<p><a href="https://blog.csdn.net/huang2017/article/details/69665057" target="_blank" rel="noopener">https://blog.csdn.net/huang2017/article/details/69665057</a></p>
<p><a href="https://blog.csdn.net/huang2017/article/details/69666154" target="_blank" rel="noopener">https://blog.csdn.net/huang2017/article/details/69666154</a></p>
<blockquote>
<p>将 sphinx 服务添加到 windows 服务：</p>
</blockquote>
<p><code>./searchd.exe --install -c sphinx.conf --servicename s</code></p>
<p><a href="https://blog.csdn.net/design321/article/details/8895712" target="_blank" rel="noopener">https://blog.csdn.net/design321/article/details/8895712</a></p>
<blockquote>
<p>sphinx 使用：</p>
</blockquote>
<p><a href="https://blog.csdn.net/u010837612/article/details/70827481" target="_blank" rel="noopener">https://blog.csdn.net/u010837612/article/details/70827481</a></p>
<blockquote>
<p>中文支持（linux 系统）</p>
</blockquote>
<p><a href="http://www.xuejiehome.com/blread-1283.html" target="_blank" rel="noopener">http://www.xuejiehome.com/blread-1283.html</a></p>
<blockquote>
<p>中文支持（windows 系统）— 默认已能支持中文</p>
</blockquote>
<p><a href="http://www.phpernote.com/php-template-framework/284.html" target="_blank" rel="noopener">http://www.phpernote.com/php-template-framework/284.html</a></p>
<blockquote>
<p>其他</p>
</blockquote>
<p><a href="https://my.oschina.net/guyson/blog/283576" target="_blank" rel="noopener">https://my.oschina.net/guyson/blog/283576</a></p>

		</div>

		
			<div class="art-item-footer">
					
						<span class="art-item-left"><i class="icon icon-chevron-thin-left"></i> <a href="/2018/05/15/movie-wxsnr/" rel="prev" title="电影《闻香识女人》">
							电影《闻香识女人》
						</a>上一篇文章</span>
					
					
						<span class="art-item-right">下一篇文章：<a href="/2018/05/09/vscode-xdebug/" rel="next"  title="【VSCode插件】xdebug开发调试PHP">
							【VSCode插件】xdebug开发调试PHP
						</a><i class="icon icon-chevron-thin-right"></i></span>
					
			</div>
		

		
			

    <div class="recommended_posts">
        <h3>推荐文章</h3>
        <ul>
            
                <li><a href="http://blog.i2arch.tech/2018/05/16/php-无限级分类/">PHP实现无限级分类 -- path标识</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/05/15/movie-wxsnr/">电影《闻香识女人》</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/05/09/vscode-xdebug/">【VSCode插件】xdebug开发调试PHP</a></li>
            
                <li><a href="http://blog.i2arch.tech/2018/05/08/php-booklist/">PHP书单</a></li>
            
        </ul>
    </div>


		
	</section>

	

</article>
<script>
	window.subData = {
		title: 'windows7使用Sphinx+PHP+MySQL详细介绍',
		tools: true
	}
</script>

      </div>

      <!-- 右侧边栏组件 -->
      <aside class='l_side'>
        
      </aside>

      <div class="gotop">
          
    <a href='javascript:void(0)'>
        <i class="icon icon-arrow_upward flat-box">
        </i>
    </a>

      </div>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/zqunor" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://www.cnblogs.com/zqunor" class="social zhihu"
          target="_blank" rel="external">
          <span class="icon icon-zhihu"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a>  Powered by<a href="https://blog.i2arch.tech/" target="_blank">程序小工</a>.</div>

</footer>

  <script>setLoadingBarProgress(80);</script>
  

    <script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
    <script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
    <script src="/js/jquery.fitvids.js"></script>
      <script>
        var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
        var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
        var ALGOLIA_API_KEY = "";
        var ALGOLIA_APP_ID = "";
        var ALGOLIA_INDEX_NAME = "";
        var AZURE_SERVICE_NAME = "";
        var AZURE_INDEX_NAME = "";
        var AZURE_QUERY_KEY = "";
        var BAIDU_API_ID = "fca5b83cf69f752ddc96a61ef64a3659";
        var SEARCH_SERVICE = "hexo";
        var ROOT = "/" || "/";
        if (!ROOT.endsWith('/')) ROOT += '/';
      </script>
      <script src="/js/search.js"></script>
        <script src="/js/app.js"></script>
  <script>setLoadingBarProgress(100);</script>
</body>
</html>
